package cn.laixueit;

import cn.laixueit.mapper.StudentMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

public class TestMyBatis {
    public static void main(String[] args) throws IOException {
        // 加载核心配置文件
        InputStream stream = Resources.getResourceAsStream("mybatis.xml");

        // 得到会话工厂
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);
        // 打开一个会话

        SqlSession session = sqlSessionFactory.openSession(true);

//        List<Object> list = session.selectList("test.findall");

//        System.out.println(list);

//        Student one = session.selectOne("test.findbyid");

//        System.out.println(one);

//        Object o = session.selectOne("test.findbyid", 2);
//        System.out.println(o);


        StudentMapper mapper = session.getMapper(StudentMapper.class);
//        System.out.println(mapper.findall());
//        System.out.println(mapper.findbyid(2));

//        Student student = new Student();
//        student.setUsername("建国");
//        student.setAddress("武汉");
//        mapper.save(student);
//        int i = mapper.delbyid(5);

//        Student student = new Student();
//        student.setId(4);
//        student.setUsername("建党");

//      student.setAddress("中华人民共和国");

//        int i = mapper.update(student);


//        System.out.println(i);


//        Student student = new Student();
//        student.setUsername("三");
//        List<Student> studentList = mapper.findListName2(student);
//        studentList.forEach(System.out::println);

//        int pagesize= 2;
//
//        Page page = new Page();
//        page.setStartPage((2-1)*pagesize);// 0  1,2
//        page.setPageSize(pagesize);
//        List<Student> studentList = mapper.findPage(page);


//        HashMap<String, Integer> map = new HashMap<>();
//        map.put("startPage",2);
//        map.put("pageSize",2);
//        List<Student> studentList = mapper.findPage2(map);


//        Student student = new Student();
//        student.setUsername("三");
//        List<Student> studentList = mapper.findListName2(student);
//        studentList.forEach(System.out::println);


//        System.out.println(mapper.findbyid(6));


//        List<Student> studentList = mapper.findListName4("三");


//        HashMap<String, Object> map = new HashMap<>();
//
//
//        Student student = new Student();
//        student.setUsername("三");
//        student.setAddress("武汉");
//        map.put("student",student);
//
//        map.put("startPage",0);
//        map.put("pageSize",2);
//
//        List<Student> studentList = mapper.findPageCondition(map);
//        studentList.forEach(System.out::println);


//        Student student = new Student();
//        student.setUsername("张三");
//        student.setAddress("武汉");
//        List<Student> studentList = mapper.findbyname(student);
//
//        System.out.println("stu = " + studentList);

//        Student student = new Student();
//        student.setUsername("张三");
//        student.setAddress("武汉");
//        List<Student> studentList = mapper.findbyname2(student);
//
//        System.out.println("stu = " + studentList);

//        long startTime = System.currentTimeMillis();

        /*for (int i = 0; i < 10000; i++) {
            Student student = new Student();
            student.setUsername("张"+(i+1));
            student.setAddress( (char) (Math.random()*26+97) +""+(char) (Math.random()*26+97) +"" +(char) (Math.random()*26+97) +""  );
            mapper.save(student);
        }*/

//        ArrayList<Student> list = new ArrayList<>();
//
//        for (int i = 0; i < 100000; i++) {
//            Student student = new Student();
//            student.setUsername("张"+(i+1));
//            student.setAddress( (char) (Math.random()*26+97) +""+(char) (Math.random()*26+97) +"" +(char) (Math.random()*26+97) +""  );
//            list.add(student);
//        }
//        mapper.saveList(list);
//
//        long endTime = System.currentTimeMillis();
//        System.out.println(endTime-startTime);


//        String str = "[]";
//        JSONArray jsonArray = JSON.parseArray(str);
//        Object[] array = jsonArray.toArray();
//
//        for (Object o : array) {
//            System.out.println(JSON.parseObject(o.toString(), Map.class));
//        }
//
//        ArrayList<String> images = new ArrayList<>();
//
//
//
//        JSONArray jsonArrays = JSON.parseArray(str);
//        for (Object o : jsonArrays.toArray()) {
//            HashMap map = JSON.parseObject(o.toString(),HashMap.class);
//            String url = (String) map.get("url");
//            images.add(url);
//        }
//
//        System.out.println(images);

        ArrayList<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(30845);
        list.add(30846);
        list.add(30847);
        list.add(30848);
        list.add(30849);

        int count = mapper.delList(list);
        System.out.println(count);

    }
}
